#!/bin/bash

fitfile=$1

if [ $# -ne 1  ]
then
	echo "usage: xot.sh a.fit"
	exit 0
fi
Dir_data=`pwd`
Dir_gaia2=~/workspace/gaia_dr2/cached-catalog/gaia_dr2/merge-60cm.csv
FoV=0.4
MAG=19.0

if test -r reduc
then
	rm -rf reduc
fi


   AstroCaliFile=`echo $fitfile | sed 's/\.fit/.new/g'`
    OUTPUT=`echo $fitfile | sed 's/\.fit/.sex/g'`
    OUTPUT_limit=`echo $fitfile | sed 's/\.fit/.sex_limit/g'`
    OUTPUT_limit_new=`echo $fitfile | sed 's/\.fit/.sex_new_limit/g'`
    OTfinalres_limit=`echo $fitfile | sed 's/\.fit/_otfinalres_withlimit.txt/g'`
    prefix=`echo $fitfile | sed 's/\.fit//g'`
    OUTPUT_new=`echo $fitfile | sed 's/\.fit/.sexnew/g'`
    TempUSNORADEC=`echo $fitfile | sed 's/\.fit/_tempRADEC.txt/g'`
    TempUSNOXY=`echo $fitfile | sed 's/\.fit/_tempXY.txt/g'`
    otxy=`echo $fitfile | sed 's/\.fit/_otxy.txt/g'`
    MonitorFile=`echo $fitfile | sed 's/\.fit/_res.txt/g'`
    matchedfile=`echo $fitfile | sed 's/\.fit/_KnownObjxy.txt/g'`
    compstarsfile=`echo $fitfile | sed 's/\.fit/_compstars.txt/g'`
    OTfile=`echo $fitfile | sed 's/\.fit/_ot.txt/g'`
    checkstarfile=`echo $fitfile | sed 's/\.fit/_checkstarrealxy.txt/g'`
    deltaMagfile=`echo $fitfile | sed 's/\.fit/_deltaMag.txt/g'`
    OTfinalres_withdis=`echo $fitfile | sed 's/\.fit/_otfinalres_withdis.txt/g'`
    OTfinalres=`echo $fitfile | sed 's/\.fit/_otfinalres.txt/g'`
    OTfinalresDiscal=`echo $fitfile | sed 's/\.fit/_otfinalresDisCal.txt/g'`
    OTFieldxyForTvMark=`echo $fitfile | sed 's/\.fit/_otxyfortvmark.txt/g'`
    OTFieldxy=`echo $OTid | awk '{print($1".otxy")}'`
    OTTriggerxy=`echo $OTid | awk '{print($1"_trigger.otxy")}'`
    OTFieldxy_ref=`echo $OTid | awk '{print($1".otxyref")}'`
    OTFieldTemp=`echo $OTid | awk '{print($1".tempcomp")}'`
    OTFieldUSNO=`echo $OTid | awk '{print($1".tempusno")}'`
    OTWCSfit=`echo $OTid | awk '{print($1"_wcs.new.fit")}'`
    RATT=`gethead $fitfile "objctra"`
    DECTT=`gethead $fitfile "objctdec"`




#xlimit.sh $fitfile

LIMITMAG=`gethead $fitfile "LIMITMAG" `
MAG=`echo $LIMITMAG | awk '{print($1+1)}'`


NAXIS1=`gethead $fitfile "NAXiS1"`
NAXIS2=`gethead $fitfile "NAXiS2"`
xcenter=`gethead $fitfile "NAXiS1" | awk '{print($1/2)}'`
ycenter=`gethead $fitfile "NAXiS2" | awk '{print($1/2)}'`

RA=`xy2sky -d $fitfile $xcenter $ycenter | awk '{print($1)}'`
DEC=`xy2sky -d $fitfile $xcenter $ycenter | awk '{print($2)}'`
python ~/software/soft60cm/gwac_search.py ra=$RA dec=$DEC fieldwidth=$FoV  mag=$MAG mergedFileName=merge-60cm.csv
cat $Dir_gaia2 | grep -v "source" |  tr ',' ' ' |  awk '{if($9<limitmag)print($4,$5)}' limitmag=$MAG >gaia2_source.txt
sky2xy $fitfile @gaia2_source.txt | grep -v "off" | awk '{print($5,$6)}'>gaia.txt

mkdir reduc
cp $fitfile reduc
cd reduc
cp ~/software/soft60cm/* ./
cp ../gaia.txt ./

Detect_sigma=1.5
ejmax=10
sex $fitfile  -c  daofind.sex  -DETECT_THRESH $Detect_sigma -ANALYSIS_THRESH $Detect_sigma -CATALOG_NAME $OUTPUT
cat $OUTPUT | awk '{if($1>ejmax && $2>ejmax && $1<(xccdsize-ejmax) && $2<(yccdsize-ejmax) && $4<0.2 && $5<0.5)print($1,$2,$3,$4,$5)}' ejmax=$ejmax xccdsize=$NAXIS1 yccdsize=$NAXIS2 >newobj.txt

xcross30cmOT  #input files are gaia.txt and newobj.txt,   outputfile is newOT.txt
if test -s newOT.txt
then
	echo "have new OT found in the $fitfile"
	xy2sky $fitfile @newOT.txt >newOT_sky.txt
	paste newOT.txt newOT_sky.txt | column -t >$OTfile
	echo "$OTfile"
	mv $OTfile ../
else
	echo "no any new OT found in the $fitfile"
	echo "The limit mag is $LIMITMAG"
fi
cd ../
rm -rf reduc


